package com.jk.bgorder.service;

import com.jk.bgorder.dao.GoodsDao;
import com.jk.bgorder.entity.IndentEntity;
import com.jk.damaicommon.entity.GoodsEntity;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 文件名称:IndentService
 *
 * @Description //TODO $
 * @Date $ $
 * 作者 郭英杰
 * Copyright (c) 2021, yuxy123@gmail.com All Rights Reserved.</pre>
 */
@Service
public class IndentService {

    @Autowired
    private MongoTemplate mongoTemplate;


    @Autowired
    private GoodsDao goodsDao;



    ////查询未付款
    public List<IndentEntity> pandingOrders(IndentEntity indent) {
        Query query = new Query();
      /*  if (indent.getOrderSort()==1){
            Sort ss = new sort(Sort.Direction.DESC,"orderSort");
            Order orderSort = new Order(Sort.Direction.DESC, "orderSort");
            query.with( new Sort(orderSort));
        }
*/
        query.addCriteria(Criteria.where("orderCondition").is(0));
        List<IndentEntity> indentEntities = mongoTemplate.find(query, IndentEntity.class);
        /*List<IndentEntity> all = mongoTemplate.findAll(IndentEntity.class);*/

        return indentEntities;
    }
    //新增
    public void addtest(IndentEntity indent) {
        mongoTemplate.save(indent);
    }
    //查询全部
    public List<IndentEntity> allOrders(IndentEntity indent) throws ParseException {
        Query query = new Query();
      /*  SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd");
        Date starDate = sim.parse(indent.getOrderStartTime());
        if (StringUtils.isNotBlank(indent.getOrderStartTime()) && StringUtils.isNotBlank(indent.getOrderEndTime())){
            query.addCriteria(Criteria.where("orderDate").lte(indent.getOrderStartTime()).gte(indent.getOrderEndTime()));
        }else{
            if (StringUtils.isNotBlank(indent.getOrderStartTime())){
                query.addCriteria(Criteria.where("orderDate").gte(starDate));
            }
            if ( StringUtils.isNotBlank(indent.getOrderEndTime())){
                query.addCriteria(Criteria.where("orderDate").lte(indent.getOrderEndTime()));
            }
        }*/
        List<IndentEntity> indentEntities = mongoTemplate.find(query, IndentEntity.class);
        return  indentEntities;
    }
    //回显
    public IndentEntity orderQuery(String id) {
        IndentEntity id1 = mongoTemplate.findById(id, IndentEntity.class);
        return id1;
    }
    //查询预期的商品
    public List<GoodsEntity> expectGoods(GoodsEntity goods) {
       List<GoodsEntity> list= goodsDao.expectGoods(goods);
        return list;
    }
}
